package com.ch.base.result;

import com.ch.base.enums.CodeEnum;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

import java.util.LinkedHashMap;

public class Result<T> extends LinkedHashMap<String, Object> {

    /**
     *
     */
    private static final long serialVersionUID = 1L;
    //错误码
    private String code;
    //错误信息
    private String msg;
    //具体的内容
    private T data;

    public Result() {

    }

    public Result(String code, String msg) {

        this.put("code", code);
        this.put("msg", msg);
    }

    public Result(T data) {

        this.put("code", CodeEnum.SUCCESS.getCode());
        this.put("msg", CodeEnum.SUCCESS.getMsg());
        this.put("data", data);
    }

    public Result(T data, Long pageCount) {

        this.put("code", CodeEnum.SUCCESS.getCode());
        this.put("msg", CodeEnum.SUCCESS.getMsg());
        this.put("data", data);
        this.put("pageCount", pageCount);
    }

    public String getCode() {

        return code;
    }

    public void setCode(String code) {

        this.code = code;
    }

    public String getMsg() {

        return msg;
    }

    public void setMsg(String msg) {

        this.msg = msg;
    }

    public T getData() {

        return data;
    }

    public void setData(T data) {

        this.data = data;
    }

    public static <T> Result<T> create(String code, String msg) {

        return new Result<>(code, msg);
    }

    public static <T> Result<T> create(T data) {

        return new Result<T>(data);
    }

    public static <T> ResultPage<T> createPage(String code, String msg) {

        return new ResultPage<>(code, msg);
    }

    public static <T> ResultPage<T> createPage(T data) {

        return new ResultPage<>(data);
    }

    public static <T> ResultPage<T> createPage(T data, Integer pageIndex, Integer pageSize, Long pageCount) {

        return new ResultPage<>(data, pageIndex, pageSize, pageCount);
    }

    public static <T> ResultPage<T> createPage(T data, Long pageCount) {

        return new ResultPage<>(data, pageCount);
    }

    @Override
    public String toString() {

        return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
    }
}
